
//
//  LeeMyFavoritesViewController.m
//  firstpoint
//
//  Created by 李雪虎 on 2017/7/7.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "LeeMyFavoritesViewController.h"
#import "LeeMyFaoritesTableViewCell.h"
@interface LeeMyFavoritesViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    int page;
}
@property(nonatomic,strong)UITableView *FavoritesTabelView;
@property(nonatomic,strong)NSMutableArray *bigMutArr;
@end

@implementation LeeMyFavoritesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    page =0;
    self.title = @"我的收藏";
    self.view.backgroundColor = [UIColor whiteColor];
    _bigMutArr = [NSMutableArray array];
    [self httpRquestUrl];
    [self addMyFavoritesTableView];//添加表
    // Do any additional setup after loading the view.
}
//添加表视图
-(void)addMyFavoritesTableView{
    //表格
    _FavoritesTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, width_screen, height_screen-40-64) style:UITableViewStyleGrouped];
    _FavoritesTabelView.backgroundColor = [UIColor clearColor];
    _FavoritesTabelView.showsVerticalScrollIndicator = NO;//隐藏滑动条
    //    _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _FavoritesTabelView.separatorColor = COLOR(229, 230, 231, 1);//分割线颜色
    _FavoritesTabelView.delegate = self;
    _FavoritesTabelView.dataSource = self;
    [_FavoritesTabelView registerNib:[UINib nibWithNibName:@"LeeMyFaoritesTableViewCell" bundle:nil] forCellReuseIdentifier:@"OenCell"];
    [self.view addSubview:_FavoritesTabelView];
    
}
#pragma mark --tableView 代理方法
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 15)];
    view.backgroundColor = COLOR(229, 230, 231, 1);
    return view;
}
//多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _bigMutArr.count;
}
//几个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LeeMyFaoritesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OenCell"];
    if (_bigMutArr.count>0) {
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",_bigMutArr[indexPath.row][@"name"]];

        cell.yuanbaoLabel.text = [NSString stringWithFormat:@"%@ 元宝",_bigMutArr[indexPath.row][@"ingot"]];
        cell.jinbiLabel.text = [NSString stringWithFormat:@"%@ 金币",_bigMutArr[indexPath.row][@"gold"]];
        cell.deleteBtn.tag = 10+indexPath.row;
        [cell.deleteBtn addTarget:self action:@selector(deleteBtnShoppingClicked:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}
//删除藏品
-(void)deleteBtnShoppingClicked:(UIButton *)button{

    NSString *str = [NSString stringWithFormat:@"%@",_bigMutArr[button.tag-10][@"id"]];
    [self deleletRquestUrlremoveFavorites:str];
}
//cell 分割线 两端封头 实现这个两个方法 1
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
#pragma mark---RquestURL
-(void)httpRquestUrl{
    
    [HTTPURL postRequest:LINK_BASE_URL(@"favorites/getPageByUserId.api") parameters:@{@"pageIndex":@(page),@"userId":userid,@"sourceType":@"2"} success:^(NSURLSessionDataTask *task, id responseObject) {
       
        NSDictionary *dic = [PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        NSArray *array = [NSArray array];
        array = dic[@"rows"];
        [_bigMutArr addObjectsFromArray:array];
        [_FavoritesTabelView reloadData];
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
//删除藏品
-(void)deleletRquestUrlremoveFavorites:(NSString *)string{

    __weak typeof(self) weakSelf = self;
    [HTTPURL postRequest:LINK_BASE_URL(@"favorites/removeFavorites.api") parameters:@{@"id":string,@"userId":userid} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"retFlag"]integerValue]==1) {
            _bigMutArr = [NSMutableArray array];
            [weakSelf httpRquestUrl];
        }
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
